﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using PrinterBudi.Persistence;
using System.Globalization;

namespace PrinterBudi.Web.Controllers
{
    public class UploadController : Controller
    {
        //
        // GET: /Upload/
        [HttpPost]
        public ActionResult Index(HttpPostedFileBase file, string address, string printDesc, 
            string paperType, string paperSize, string numCopy)
        {
            
            try
            {
                int.Parse(numCopy);
            }
            catch (FormatException)
            {
                return Redirect("/Home/Upload?error=1");
            }

            //string[] cek = file.FileName.Split('.');
            //if (!cek[cek.Length - 1].Equals("doc") || !cek[cek.Length - 1].Equals("docx") || !cek[cek.Length - 1].Equals("pdf"))
            //{
            //    ViewBag.Error = "invalid file type";
            //    ViewBag.address = address;
            //    ViewBag.printDesc = printDesc;
            //    ViewBag.numCopy = numCopy;
            //    return Redirect("/Home/Upload?error=1");
            //}
            
            file.SaveAs(Path.Combine(Server.MapPath("~/Content/Uploads"), file.FileName));
            PBUser user = PBUserModel.Read((int)Session["userId"]);
            //IFormatProvider format = new CultureInfo("en-US", true);
            //DateTime sekarang = Convert.ToDateTime(DateTime.Now, format);
            string dAddress = (address.Equals("")) ? user.Address : address;
            PBTransactionModel.Create(DateTime.Now, file.FileName, "Queued", printDesc, dAddress, 0, paperType, paperSize, int.Parse(numCopy), new DateTime(1970, 1, 1, 0, 0, 0), user);
            return View();
        }

    }
}
